package com.congwulin.wechat.service;

import com.congwulin.wechat.entity.common.SuperVO;
import com.congwulin.wechat.entity.order.OrderBillVO;
import com.congwulin.wechat.entity.order.OrderBodyVO;
import com.congwulin.wechat.entity.order.OrderHeadVO;
import com.congwulin.wechat.entity.product.Product;
import com.congwulin.wechat.entity.user.User;
import com.congwulin.wechat.mapper.order.OrderBodyMapper;
import com.congwulin.wechat.mapper.order.OrderBodyPersistent;
import com.congwulin.wechat.mapper.order.OrderHeadMapper;
import com.congwulin.wechat.mapper.order.OrderHeadPersistent;
import com.congwulin.wechat.util.ExceptionUtils;
import com.congwulin.wechat.util.service.bill.BillPersistent;
import com.congwulin.wechat.util.service.bill.BillSaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {

    @Autowired
    private OrderHeadMapper headMapper;

    @Autowired
    private OrderBodyMapper bodyMapper;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    public List<OrderBillVO> save(List<OrderBillVO> list){
        List<SuperVO> bodyList = list.get(0).getChildren(OrderBodyVO.class);
        Map<String,Integer> stockMap = new HashMap<String, Integer>();
        Map<String,OrderBodyVO> bodyMap = new HashMap<String, OrderBodyVO>();
        for(SuperVO vo: bodyList){
            OrderBodyVO bodyVO = (OrderBodyVO)vo;
            stockMap.put(bodyVO.getCode(),bodyVO.getNum());
            bodyMap.put(bodyVO.getCode(),bodyVO);
        }
        List<Product> productlist = productService.selectProductByCodes(new ArrayList<String>(stockMap.keySet()));
        double sum = 0;
        for(Product product: productlist){
            String code = product.getCode();
            int num = stockMap.get(code);
            if(product.getStock()-num<0){
                ExceptionUtils.wrapBusinessException(product.getName()+"的库存小于"+num+", 无法下单！！！");
            }else{
                product.setStock(product.getStock()-num);
                product.setStatus(1);
                OrderBodyVO bvo = bodyMap.get(code);
                bvo.setPrice(product.getPrice());
                bvo.setAllprice(new BigDecimal(product.getPrice().doubleValue()*bvo.getNum()));
                sum = sum + product.getPrice().doubleValue()*bvo.getNum();
            }
        }
        ((OrderHeadVO)list.get(0).getHead()).setPrice(new BigDecimal(sum));
        productService.saveProduct(productlist);
        BillPersistent billPersistent = new BillPersistent();
        billPersistent.regiter(OrderHeadVO.class,new OrderHeadPersistent(headMapper));
        billPersistent.regiter(OrderBodyVO.class, new OrderBodyPersistent(bodyMapper));
        BillSaveService service = new BillSaveService(billPersistent);
        List<OrderBillVO> billLists = (List<OrderBillVO>)service.save(list);
        String creator = ((OrderHeadVO) billLists.get(0).getHead()).getCreator();
        User user = userService.getUserByID(creator);
        user.setIntegral(user.getIntegral().intValue()+((OrderHeadVO) billLists.get(0).getHead()).getPrice().intValue());
        user.setStatus(1);
        List<User> userList = new ArrayList<User>();
        userList.add(user);
        userService.saveUser(userList);
        return billLists;
    }

    public List<OrderHeadVO> getList(HashMap searchParams){
        List<OrderHeadVO> list = headMapper.selectAllForUserByPage(searchParams);
        return list;
    }

    public List<OrderBodyVO> getBodyList(HashMap searchParams){
        List<OrderBodyVO> list = bodyMapper.selectAllForOrderIDByPage(searchParams);
        return list;
    }
}
